<template>
  <div id="teachingGroupPage">
    <!-- 表格 -->
    <a-table
      bordered
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH-mm-ss') }}
        </template>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { listStudentGroupByPageByTeacherUsingPost } from '@/api/studentGroupController.ts'

const columns = [
  {
    title: '班级名称',
    dataIndex: 'className',
  },
  {
    title: '年级',
    dataIndex: 'grade',
  },
  {
    title: '班主任',
    dataIndex: 'teacherName',
  },
  {
    title: '班级人数',
    dataIndex: 'studentNum',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
]

// 定义数据
const dataList = ref<API.StudentGroup[]>([])
const total = ref(0)

// 搜索条件
const searchParam = reactive<API.StudentGroupQueryByTeacherDto>({
  current: 1,
  pageSize: 10,
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParam.current,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共${total}条`,
  }
})

const doTableChange = (page: any) => {
  searchParam.current = page.current
  searchParam.pageSize = page.pageSize
  fetchData()
}

// 获取数据
const fetchData = async () => {
  const res = await listStudentGroupByPageByTeacherUsingPost({
    ...searchParam,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

const loginUserStore = useLoginUserStore()

onMounted(() => {
  const teacherNumber = loginUserStore.loginUser.userAccount
  searchParam.teacherNumber = teacherNumber
  fetchData()
})

</script>

<style scoped>
#teachingGroupPage {
  padding: 16px;
}
</style>
